package flysearch.ui;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.ext.swing.SwingTextField;
import javafx.ext.swing.SwingButton;
import flysearch.core.Indexer;
import flysearch.core.Searcher;
import java.io.File;
import javafx.ext.swing.SwingLabel;

def INDEX_PATH = "E:\\document\\techdoc\\";
def DATA_PATH = "C:\\Temp\\";

def indexer = Indexer{
    indexPath: INDEX_PATH;
    dataPath: DATA_PATH;
}

def searcher = Searcher{
    dataPath: DATA_PATH;
    resultCount:10;
}


//var num = indexer.index();
//println(num);

var mainStage : Stage;
var keywordTextField : SwingTextField;
var searchButton : SwingButton;
var resultList: File[];

mainStage.toFront();

keywordTextField = SwingTextField {
    translateX: 10
    translateY: 10
    columns: 34
    height: 40
    text: "Please input key words here"
    editable: true
    font: Font{size:16}
}

searchButton = SwingButton {
    translateX: 475
    translateY: 10
    width: 100
    height: 40
    text: "GO!"
    font: Font{size:16}
    action: function() {
        //移除原来的结果
        var results = mainStage.scene.content[2..];
        for(result in results){
            delete result from mainStage.scene.content;
        }

        //查询
        resultList = searcher.search(keywordTextField.text);

        //用SwingLabel显示搜索结果
        var i = 0;
        for(file in resultList){
            var pathSegment = resultList[i].getPath().split("\\\\");
            var label = SwingLabel{
                translateX: 10
                translateY: 56 + 20*i
                text : "{resultList[i].getName()} -- {pathSegment[0]}\\{pathSegment[1]}...{pathSegment[sizeof pathSegment-1]}";
                font: Font{size:12}
            };
            insert label into mainStage.scene.content;
            i++;
        }

        //Stage高度自适应
        mainStage.height = 96 + 20*i;
    }
}

mainStage = Stage {
    title: "Setting your desktop search engine"
    width: 600
    height: 96
    scene: Scene {
        content: [
            keywordTextField,
            searchButton
        ]
    }
}
